codecombatfandomcom-20200213-history
Return to Thornbush Farm
Game Descripition Part A Ogres are invading again, and the only cure is more parameters! Part B Those ogres bought the farm. Hints Page 1 Your functions can define more than one parameter. def maybeBuildTrap(x, y): # When this function is called below, # x will be 43, y will be 50 maybeBuildTrap(43, 50) Page 2 Just like moveXY accepts two arguments, the functions you create can define more than one parameter! def maybeBuildTrap(x, y): # When this function is called below, # x will be 43, y will be 50 maybeBuildTrap(43, 50) Parameters vs. Arguments So why do we sometimes call things parameters, and sometimes call things arguments? A parameter is the thing you define in your function. An argument is the actual value that is passed into the function when it's called by your program! Page 3 Strategy Python A strategy by Anaphalis: # The function maybeBuildTrap defines TWO parameters! def maybeBuildTrap(x, y): # Use x and y as the coordinates to move to. hero.moveXY(x, y) enemy = hero.findNearestEnemy() if enemy: pass # Use buildXY to build a "fire-trap" at the given x and y. hero.buildXY("fire-trap", x, y) while True: # This calls maybeBuildTrap, with the coordinates of the top entrance. maybeBuildTrap(43, 50) # Now use maybeBuildTrap at the left entrance! maybeBuildTrap(25, 34) # Now use maybeBuildTrap at the bottom entrance! maybeBuildTrap(43, 20) JavaScript A strategy by Omnicrunch: // The function maybeBuildTrap defines TWO parameters! function maybeBuildTrap(x, y) { // Use x and y as the coordinates to move to. hero.moveXY(x, y); var enemy = hero.findNearestEnemy(); if(enemy) { // Use buildXY to build a "fire-trap" at the given x and y. hero.buildXY("fire-trap", x, y); } } while(true) { // This calls maybeBuildTrap, with the coordinates of the top entrance. maybeBuildTrap(43, 50); // Now use maybeBuildTrap at the left entrance! maybeBuildTrap(25, 34); // Now use maybeBuildTrap at the bottom entrance! maybeBuildTrap(43, 20); } CoffeeScirpt A strategy by Anaphalis: # The function maybeBuildTrap defines TWO parameters! @maybeBuildTrap = (x, y) -> # Use x and y as the coordinates to move to. @moveXY(x, y) enemy = @findNearestEnemy() if enemy # Use buildXY to build a "fire-trap" at the given x and y. @buildXY("fire-trap", x, y) while true # This calls maybeBuildTrap, with the coordinates of the top entrance. @maybeBuildTrap(43, 50) # Now use maybeBuildTrap at the left entrance! @maybeBuildTrap(25, 34) # Now use maybeBuildTrap at the bottom entrance! @maybeBuildTrap(43, 20) Lua A strategy by Anaphalis: -- The function maybeBuildTrap defines TWO parameters! local maybeBuildTrap = function(x, y) -- Use x and y as the coordinates to move to. self:moveXY(x, y) local enemy = self:findNearestEnemy() if enemy then self:say("I should build!") -- Use buildXY to build a "fire-trap" at the given x and y. self:buildXY("fire-trap", x, y) end end while true do -- This calls maybeBuildTrap, with the coordinates of the top entrance. maybeBuildTrap(43, 50) -- Now use maybeBuildTrap at the left entrance! maybeBuildTrap(25, 34) -- Now use maybeBuildTrap at the bottom entrance! maybeBuildTrap(43,50) end See also * Return to Thornbush Farm A (practice) * Return to Thornbush Farm B (practice) * JavaScript: Return to Thornbush Farm